<template>
  <!-- 添加或修改生产记录明细对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="goodsModel">
        <el-input v-model="queryParams.goodsModel" placeholder="请输入产品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品单位" prop="goodsUnit">
        <el-input v-model="queryParams.goodsUnit" placeholder="请输入产品单位"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="操作工" prop="douser">
        <el-input v-model="queryParams.douser" placeholder="请输入操作工"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="sort"/>
      <el-table-column label="生产单号" align="center" prop="produceNos"/>
      <el-table-column label="产品编码" align="center" prop="goodsCode"/>
      <el-table-column label="产品名称" align="center" prop="goodsName"/>
      <el-table-column label="产品型号" align="center" prop="goodsModel"/>
      <el-table-column label="产品单位" align="center" prop="goodsUnit"/>
      <el-table-column label="数量" align="center" prop="count"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.produce_record_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作工" align="center" prop="douser"/>
      <el-table-column label="自制入库明细" align="center" prop="doremark"/>
      <el-table-column label="退回原因" align="center" prop="refuseReason"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleQualityTest(scope.row)">质量检验</el-button>
          <el-button v-show="scope.row.status === '2'" size="mini" type="text" icon="el-icon-circle-close"
                     @click="handleStoreReject(scope.row)" v-hasPermi="['develop:ProduceRecord:store']">库管弃审
          </el-button>
          <el-button size="mini" v-show="scope.row.status === '4'"  type="text" icon="el-icon-circle-close"
                     @click="handleStoreReturn(scope.row)" v-hasPermi="['develop:ProduceRecord:store']">入库退回
          </el-button>
          <el-popconfirm :title="`确认通过当前数据项？`" @confirm="handleStoreIn(scope.row)"
                         v-if="scope.row.status === '6'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-close"
                       v-hasPermi="['develop:ProduceRecord:store']">库管入库
            </el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
    </div>

    <factory-test-quality-produce ref="qualityTestProduce" @ok="getList"> </factory-test-quality-produce>
    <produce-record-detail-refuse-form ref="refuseForm" @ok="getList"></produce-record-detail-refuse-form>
    <produce-record-detail-refuse-store-form ref="refuseStoreForm" @ok="getList"></produce-record-detail-refuse-store-form>

  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, isEmpty, updateAction} from '@/api/manage';
import moment from 'moment';
import FactoryTestQualityProduce from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityProduce.vue";
import ProduceRecordDetailRefuseForm
  from "@/views/develop/produceRecordDetail/module/ProduceRecordDetailRefuseForm.vue";
import ProduceRecordDetailRefuseStoreForm
  from "@/views/develop/produceRecordDetail/module/ProduceRecordDetailRefuseStoreForm.vue";

export default {
  name: "ProduceRecordDetailListModule",
  components: {ProduceRecordDetailRefuseStoreForm, ProduceRecordDetailRefuseForm, FactoryTestQualityProduce},
  dicts:["produce_record_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产记录明细表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        produceId: null,
        produceNos: null,
        status: null,
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        goodsUnit: null,
        count: null,
        douser: null,
        doremark: null,
        orderByColumn: 'a.sort',
        isAsc: "asc"
      },
      url: {
        list: '/produceRecordDetail/produceRecordDetail/list',
        getById: '/produceRecordDetail/produceRecordDetail/',
        add: '/produceRecordDetail/produceRecordDetail/add',
        edit: '/produceRecordDetail/produceRecordDetail/edit',
        storeReturn: '/produceRecordDetail/produceRecordDetail/storeReturn/',
        storeIn: '/produceRecordDetail/produceRecordDetail/storeIn/',
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        produceId: null,
        produceNos: null,
        status: "0",
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        goodsUnit: null,
        count: null,
        douser: null,
        doremark: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    showDetail(row) {
      this.reset();
      this.open = true;
      this.title = "生产记录明细";
      this.queryParams.produceId = row.id;
      this.getList();
    },

    /** 查询生产记录明细列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    handleStoreReject(row){
      this.$refs.refuseForm.handleRefuse(row);
    },


    handleStoreReturn(row){
      if(isEmpty(row.rukuDetailId)){
        this.$modal.msgError("先入库该产品的生产单！");
        return;
      }
      this.$refs.refuseStoreForm.handleRefuse(row);
    },

    handleStoreIn(row){
      getAction(this.url.storeIn + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    /** 检验批次 */
    handleQualityTest(row) {
      if(row.status==='1'){
        this.$refs.qualityTestProduce.showByProduceDetail(row,true);
      }else{
        this.$refs.qualityTestProduce.showByProduceDetail(row,false);
      }

    },


  }
};
</script>
